//
//  WBHomeViewController.swift
//  微博
//
//  Created by Mstarc on 2016/11/28.
//  Copyright © 2016年 Mstarc. All rights reserved.
//

import UIKit
//定义全局常量 尽量用private 修饰 否则可以到处访问

private let cellId = "cellId"


class WBHomeViewController: WBBaseController {

  //列表数据模型
   lazy var listViewModel = WBStatusListViewModel()
    
    //加载数据
    override func loadData() {
   
        listViewModel.loadStatus(pullup: isPullup) { (isSuccess,shouldRefresh) in
            print("加载数据结束\(self.listViewModel.statusList.last?.text)")
          
            //结束刷新控件
            self.refreshZControl?.endRefreshing()
            //恢复上拉加载标记
            self.isPullup = false
            //刷新列表
            if shouldRefresh {
            self.tabelView?.reloadData()
            }
            
        }
    
     print("开始加载数据\(WBNetworkManager.shared)")
    }
    
    
    func showFriend(){
        //显示好友
        let vc = WBDemoViewController()
        //push的动作是navigation做的
        navigationController?.pushViewController(vc, animated: true)
    }

}

// MARK: - 表格数据源方法
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.取cell
        let cell = tabelView?.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        //2.设置cell
        cell?.textLabel?.text = listViewModel.statusList[indexPath.row].text
        //3.返回cell
    
        return cell!
    }
}

//MARK: - 设置界面

extension WBHomeViewController{
    
   //设置导航栏按钮 正常设置无高亮
    override func setupTabelView() {
        super.setupTabelView()
        view.backgroundColor = UIColor.red
        navaItem.leftBarButtonItem = UIBarButtonItem(title: "好友",  target: self, action: #selector(showFriend))
        //注册原型cell
        tabelView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
    
    
}

